const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: './src/app.jsx',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/dist/',
    filename: 'js/app.js'
  },
  module: {
    rules: [
        //react语法的处理
      {
        test: /\.jsx$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env','react']
          }
        }
      },
      //这里是css文件的处理
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
            fallback: "style-loader",
            use: "css-loader"
          })
      },
      //这里是sass文件的处理
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      },
      //图片的配置
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192,  //文件大于8k时才单独生成文件，小于该值则用base64编码
              name : 'resource/[name].[ext]'
            }
          }
        ]
      },
      //字体的配置
      {
            test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
            use: [
                {
                  loader: 'url-loader',
                  options: {
                    limit: 8192,  //文件大于8k时才单独生成文件，小于该值则用base64编码
                    name : 'resource/[name].[ext]'
                  }
                }
              ]
      },
    ]
  },
  devServer: {
         port:8086
       },
  plugins: [
      //处理HTML文件
      new HtmlWebpackPlugin({
          template: './src/index.html'
      }),
      //独立css文件
      new ExtractTextPlugin("css/[name].css"),//该插件解决样式载入缓慢的问题，参数为我们要生成的文件
      //提出公共模块
      new webpack.optimize.CommonsChunkPlugin({
          name : 'common',
          filename  : 'js/base.js'
      })
    ]
};